<%
' 全部ajax请求均用post来完成
class AjaxAction ' Extends CommonAction
	Private Sub Class_Intialize
		if not that.isAjax() Then	'如果不是ajax请求，中止程序运行
			'that.exit
		End If
	End Sub
	
	Private Function getModel()
		dim tableName
		tableName = that.get("table")
		'tableName = "category"		
		set getModel = M_(tableName)		
	End Function

	sub test		
		Easp.wn "test"
	End sub

	
	Sub importConditions( ByRef m,ByRef conditions )
		dim dict,key
		set dict = D_
		if ( Not isEmpty(conditions)  and isObject(conditions) ) Then			
			for each key in conditions				
				if key = "_id" Then
					dict.add m.db.getPK(),conditions(key)
					call m.db.where(dict)
				Else
					Execute "call m.db." & key & "( conditions(""" & key & """) )"
				End if
			next
		End If
		set dict = nothing
	End Sub
	
	'删除数据
	Sub remove
		on error resume next
		dim m,data,succeed : succeed = false
		set m = getModel()	
		set data = json_decode( POP_MVC.post("_data") )
		call importConditions(m, data("conditions") )
		if m.db.remove( ) Then
			succeed = true
		End if			
		that.unset data			
		call handler( m , succeed )
		Call L_("")
		that.unset m
	End Sub
	
	'修改数据中的字段
	Sub modify
		on error resume next
		dim m,data,succeed : succeed = false
		set m = getModel()	
		set data = json_decode( POP_MVC.post("_data") )
		call importConditions(m, data("conditions") )
		if m.db.SetField( data("data"),0 ) Then
			succeed = true
		End if			
		that.unset data			
		call handler( m , succeed )
		Call L_("")
		that.unset m
	End Sub
	
	'修改数据
	Sub Update
		on error resume next
		dim m,data,succeed : succeed = false
		set m = getModel()				
		if method_exists( that.get("table"),"Save" ) Then
			if m.Save() Then
				succeed = true
			End If
		else		
			set data = POP_MVC.post("")		
			call m.db.data( data )
			if m.db.create(1) Then
				if m.db.Save()>0 Then	
					succeed = true
				end If
			End if			
			that.unset data			
		End If
		
		call handler( m , succeed )
		Call L_("")
		that.unset m
	End Sub
	
	Sub handler( m,succeed )	
		if succeed Then
			call that.ajaxReturn( "恭喜您！！" )
		else			
			call that.ajaxReturn( array(m.db.error,"操作错误",0) )
		End If	
	End Sub
	

	
	' 添加数据
	Sub Save
		on error resume next
		dim m,data,succeed : succeed = false		
		set m = getModel()			
		if method_exists( that.get("table"),"Add" ) Then			
			if m.Add() Then
				succeed = true
			End If
		else		

			set data = POP_MVC.post("")		
			call m.db.data( data )
			if m.db.create(1) Then
				if m.db.Add() > 0 Then				
					succeed = true
				end If
			End if			
			that.unset data			
		End If
		call handler( m , succeed )
		Call L_("")
		that.unset m
	End Sub
	
	sub system
		that.ajaxReturn( getSysInfo() )
	End Sub
	
	'切换bool值
	Sub switchBool
		dim m,id,data,field,where
		set m = getModel()
		set data = json_decode( POP_MVC.post("_data") )
		id = data("_id")
		field = data("field")
		
		if isArray( id ) Then
			where = m.db.getPK() & " IN ( " & join(id," , ") & " )"
		Else 
			where = m.db.getPK() & " = " & id
		End if
		call m.db.execute( "UPDATE " & m.db.tableName & " SET " & field & " = Not " & field & " WHERE " & where )
		that.unset m
		that.unset data	
	End Sub
	
	'计数
	sub count
		on error resume next
		Dim m,data
		set m = getModel()
		set data = json_decode( POP_MVC.post("_data") )
		call importConditions(m, data("options") )
		call that.ajaxReturn( m.db.count() )		'ajaxReturn自动注销rs	
		that.unset m
		that.unset data
		call L_("")
	End Sub
	
	'查找一条记录
	sub findOne
		on error resume next
		Dim m,rs,data
		set m = getModel()
		set data = json_decode( POP_MVC.post("_data") )
		call importConditions(m, data("options") )
		set rs = m.db.find()
		call that.ajaxReturn( m.db.getRow(rs) )		'返回一条记录
		that.unset m
		that.unset data
		call L_("")
	End Sub
	

	' 对应的其实是Select
    sub find
		on error resume next
		Dim m,rs,data
		set m = getModel()
		set data = json_decode( POP_MVC.post("_data") )

		call importConditions(m, data("options") )
		set rs = m.db.select()
		
		call that.ajaxReturn( rs )		'ajaxReturn自动注销rs
		
		that.unset m
		that.unset data
		call L_("")
	End sub
	
	private function rs2json(rs)
		rs2json = Easp.Str.toString(rs)
		rs2json = mid(rs2json,inStr(rs2json,"["),inStrRev(rs2json,"]")-inStr(rs2json,"[")+1)
	End function
End Class
%>